<?php
/**
 * +--------------------------------------------------------------
 * | eLuoPHP
 * +--------------------------------------------------------------
 * | Copyright (c) 2016-2018 http://qxwoo.cn All rights reserved.
 * +--------------------------------------------------------------
 * | y139w [y139w@163.com]
 * +--------------------------------------------------------------
 * | 控制器基类文件
 * +--------------------------------------------------------------
 */

namespace luo;

use luo\response\Json;

class Controller
{

    /**
     * 模板引擎
     * @var null|Smarty
     */
    private $smarty;

    /**
     * 请求对象
     * @var null|Request
     */
    protected $request = null;


    /**
     * Controller constructor.
     */
    public function __construct()
    {

        // 模板引擎
        $this->smarty = Smarty::instance();
        // 请求对象
        $this->request = Request::instance();

    }

    /**
     * 返回JSON数据
     * @param $data
     * @param $code
     * @param array $header
     * @return Json
     */
    public function json($data, $code = 200, $header = []){

        $json = new Json();
        return $json->create($data, 'json', $code, $header);

    }

    /**
     * 设置模板变量
     * @param $tpl_var
     * @param null $value
     * @param bool $nocache
     * @return bool
     */
    public function assign($tpl_var, $value = null, $nocache = false){

        return $this->smarty->assign($tpl_var, $value, $nocache);

    }

    /**
     * 返回结果
     * @param null $template
     * @param null $data
     * @param null $cache_id
     * @param null $compile_id
     * @param null $parent
     * @return string
     * @throws \Exception
     * @throws \SmartyException
     */
    public function fetch($template = null, $data = null, $cache_id = null, $compile_id = null, $parent = null){

        $fetch = $this->smarty->fetch($template, $data, $cache_id, $compile_id, $parent);
        return $fetch;

    }

    /**
     * 输出结果
     * @param null $template
     * @param null $data
     * @param null $cache_id
     * @param null $compile_id
     * @param null $parent
     * @throws \Exception
     * @throws \SmartyException
     */
    public function display($template = null, $data = null, $cache_id = null, $compile_id = null, $parent = null){

        $this->smarty->display($template, $data, $cache_id, $compile_id, $parent);
        exit;

    }


}